package com.baomidou.springboot.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.springboot.dto.CarOwnerDTO;
import com.baomidou.springboot.request.CarOwnerResponse;
import com.baomidou.springboot.request.GetCarOwnerRequest;
import com.baomidou.springboot.util.AESUtil;
import com.baomidou.springboot.util.MD5Util;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.util.Date;

/**
 * @descripe：
 * @company：南京华讯方舟通讯设备有限公司
 * @author：zhoujinbing
 * @time：2021/12/6 14:20
 * @version：V1.0
 */
@RestController
@Slf4j
@Api(tags = "API-CarMoveCtronller")
public class CarMoveController {
    private static String aesKey = "c?uiGC$oyc$u#4QX~7s#";
    private static String sercret = "HBYC@2021";

    @PostMapping("/upload")
    public String testUpload(MultipartFile file)
    {
        return file.getOriginalFilename();
    }

    @PostMapping("/carmove/queryCarInfo")
    public GetCarOwnerRequest queryCarInfo(@RequestBody CarOwnerResponse response, BindingResult result) {
        try {
            log.info("请求参数:{}", response.toString());
            if (result.hasErrors()) {
                return GetCarOwnerRequest.builder()
                        .code("-1")
                        .msg("车牌不能为空")
                        .build();
            }
            String carNum = response.getCarNum();
            String city = carNum.substring(0, 1);

            CarOwnerDTO dto = new CarOwnerDTO();
            dto.setTimestamp(DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_PATTERN));
            dto.setSign(MD5Util.md5(dto.getTimestamp(), sercret));
            String phone = "13913868953";
            dto.setPhone(AESUtil.encrypt(phone, aesKey));
            dto.setCarNum(city + response.getCarNum());
            dto.setCarType(carTypePoliceRect(response.getCarType()));
            dto.setColor("白色");
            dto.setCarBrand("宝马");

            return GetCarOwnerRequest.builder()
                    .code("0")
                    .msg("查询成功")
                    .data(dto).build();
        } catch (Exception e) {
            log.error("查询车牌异常:{}",e.getMessage());
            return GetCarOwnerRequest.builder()
                    .code("-1")
                    .msg("车牌不能为空")
                    .build();
        }
    }

    private String  carTypePoliceRect(String carTypePolice){
        String carType = null;
        switch (carTypePolice){
            case "02":   //蓝牌小型汽车
                carType = "0";
                break;
            case "52":   //小型新能源汽车
                carType = "1";
                break;
            case "01":   //黄牌大型汽车
                carType = "2";
                break;
            case "51":   //大型新能源汽车
                carType = "3";
                break;
        }
        return carType;
    }
}
